/*
** main.c for  in /home/faye_m//zappy-2011/new_server/src
** 
** Made by mouhamed faye
** Login   <faye_m@epitech.net>
** 
** Started on  Fri Jul  8 17:36:14 2011 mouhamed faye
** Last update Sun Jul 10 23:17:38 2011 el-hadji-malick-ndour dieng
*/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <arpa/inet.h>
#include "types.h"
#include "../../smap/trantor/map/map.h"

int	main(int ac, char **av)
{
  T_env			*e;
  t_server_params       server;
  t_map			map;

  server.flag = 0;
  parser(&server, av, ac);
  map_init(&map, server.x, server.y, 1024);
  map_generate_resources(&map, 1024);
  map_display(&map);
  e = malloc(sizeof (T_env));
  if (e != NULL)
    {
      e->tab = malloc(sizeof (int) * getMaxFd());
      bzero(e->tab, getMaxFd());
      e->inc = 0;
      init_env(e, &server);
      set_socket(e);
      bind_socket(e, &server);
      set_fds(e);
      loop(e, &server);
      return 0;
    }
  else
    printf ("Memmory Error ...\n");
}

int             getMaxFd()
{
  struct rlimit rlp;
  int           systemCallResult;

  systemCallResult = getrlimit(RLIMIT_NOFILE, &rlp);
  if (systemCallResult == -1)
    {
      printf("ERROR : Cannot get file descriptor limit\n");
      printf("------  Impossible to allocate client table\n");
      exit(EXIT_FAILURE);
    }
  return (rlp.rlim_cur);
}
